package com.wangsb.code0403;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.Callable;

/**
 * @Description: TODO
 * @author: shenbing.wang@hand-china
 * @date: 2021年06月26日 16:39
 */
public class CopyFileCallableThread implements Callable {

    private File file;
    private File destDir;

    public CopyFileCallableThread(File file, File destDir) {
        this.file = file;
        this.destDir = destDir;
    }

    @Override
    public Object call() {
        System.out.println("线程  " + Thread.currentThread().getName() + "已经启动，原文件为" + this.file + "目标文件为" + this.destDir);
        copyFileRecursively(this.file, this.destDir);
        return null;
    }

    private void copyFileRecursively(File srcFile, File destDir) {
        if (srcFile.exists()) {
            copyFile(srcFile, destDir);
            if (srcFile.isDirectory()) {
                File[] files = srcFile.listFiles();
                if (files != null) {
                    for (File file : files) {
                        copyFileRecursively(file, new File(destDir.getPath() + "/" + srcFile.getName()));
                    }
                }
            } else {
                System.out.println("要被复制的文件或目录不存在");
            }


        }
    }

    private void copyFile(File srcFile, File destDir) {
        File newFile = new File(destDir.getPath() + "/" + srcFile.getName());
        if (srcFile.isDirectory()) {
            if (newFile.exists()) {
                System.out.println("目录" + newFile.getPath() + "已经存在");
            } else {
                System.out.println(newFile.mkdir() ? "目录" + newFile.getPath() + "创建成功" : "目录" + newFile.getPath() + "创建失败");
            }
        } else if (srcFile.isFile()) {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(srcFile));
                bos = new BufferedOutputStream(new FileOutputStream(newFile));

                byte[] bytes = new byte[1024];
                int res = 0;
                while ((res = bis.read(bytes)) != -1) {
                    bos.write(bytes, 0, res);
                }
                System.out.println("复制文件" + srcFile.getName() + "成功！");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }


    }
}
